<?php

mysql_connect ("localhost", "webluke_mpc", "mpc")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("webluke_mpc");

function form ($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
?>
<form method="POST" action="./post.php?mode=<?php if ($mode == "add") {$mode="ins";} elseif ($mode == "ins") {$mode = "edt";} elseif ($mode == "edt") {$mode = "upd";} else {$mode = "upd";} echo $mode;?>">
   <table cellpadding="0" cellspacing="0" width="700">
   <tr>
   <td width="25%">
   Recipe Name:
   </td>
   <td width="75%">
   <input type="text" name="rec_name" value="<?php echo ".$rec_name.";?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Source / By:
   </td>
   <td width="75%">
   <input type="text" name="rec_source" value="<?php echo ".$rec_source.";?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Catigory:
   </td>
   <td width="75%">
   <input type="text" name="rec_cat" value="<?php echo ".$rec_cat.";?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Main Ingredient:
   </td>
   <td width="75%">
   <input type="text" name="rec_main" value="<?php echo ".$rec_main.";?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Prep-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_prep" value="<?php echo ".$rec_prep.";?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Cook-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_cook" value="<?php echo ".$rec_cook.";?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Servings:
   </td>
   <td width="75%">
   <input type="text" name="rec_serves" value="<?php echo $rec_serves;?>" size="35" maxlength="64">
   </td>
   </tr>
   <td width="25%">
   Quick Meal:
   </td>
   <td width="75%">
   <input type="radio" name="rec_qm" <?php if($rec_qm=="1")echo "checked ";?> value="1">Yes <input type="radio" name="rec_qm" <?php if($rec_qm=="0")echo "checked ";?> value="0">No 
   </td>
   </tr>
   <tr>
   <td width="25%">
   Ingredients:
   </td>
   <td width="75%">
   <textarea name="rec_ingred" rows="5" cols="50"><?php echo $rec_ingred; ?></textarea>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Directions:
   </td>
   <td width="75%">
   <textarea name="rec_direct" rows="5" cols="50"><?php echo $rec_direct; ?></textarea>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Notes:
   </td>
   <td width="75%">
   <textarea name="rec_notes" rows="5" cols="50"><?php echo $rec_notes; ?></textarea>
   </td>
   </tr>
   <tr>
   <td>
   </td>
   <td>
   <input type="submit" value="Submit"><input type="reset" value="Reset">
   </td>
   </tr>
   </table>
</form>
   
<?php

}

function post_var($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
		 $rec_id = $_POST['rec_id'];
    	 $rec_name = $_POST['rec_name'];
		 $rec_cat = $_POST['rec_cat'];
		 $rec_main = $_POST['rec_main'];
		 $rec_prep = $_POST['rec_prep'];
		 $rec_cook = $_POST['rec_cook'];
		 $rec_serves = $_POST['rec_serves'];
		 $rec_source = $_POST['rec_source'];
		 $rec_qm = $_POST['rec_qm'];
		 $rec_ingred = $_POST['rec_ingred'];
		 $rec_direct = $_POST['rec_direct'];
		 $rec_notes = $_POST['rec_notes'];
//return $rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes;
}

function row_var ($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
		 $rec_id = $row[0];
    	 $rec_name = $row[1];
		 $rec_cat = $row[2];
		 $rec_main = $row[3];
		 $rec_prep = $row[4];
		 $rec_cook = $row[5];
		 $rec_serves = $row[6];
		 $rec_source = $row[7];
		 $rec_qm = $row[8];
		 $rec_ingred = $row[9];
		 $rec_direct = $row[10];
		 $rec_notes = $row[11];
//return $rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes;
}

function insert_sql ($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
		 $query = "INSERT INTO recipe VALUES ('', '$rec_name', '$rec_cat', '$rec_main', '$rec_prep', '$rec_cook', '$rec_serves', '$rec_source', '$rec_qm', '$rec_ingred', '$rec_direct', '$rec_notes')";
		 mysql_query($query) or die(mysql_error());
}

function update_sql ($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
		 $query = "UPDATE recipe SET 'rec_name'=$rec_name, 'rec_cat'=$rec_cat, 'rec_main'=$rec_main, 'rec_prep'=$rec_prep, 'rec_cook'=$rec_cook, 'rec_serves'=$rec_serves, 'rec_source'=$rec_source, 'rec_qm'=$rec_qm, 'rec_ingred'=$rec_ingred, 'rec_direct'=$rec_direct, 'rec_notes'=$rec_notes WHERE 'rec_id'=$rec_id";
		 mysql_query($query) or die(mysql_error());
}

function get_sql ($rec_id) {
	$query = "SELECT * FROM recipe ORDER BY rec_id";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
		 $rec_id = $row[0];
    	 $rec_name = $row[1];
		 $rec_cat = $row[2];
		 $rec_main = $row[3];
		 $rec_prep = $row[4];
		 $rec_cook = $row[5];
		 $rec_serves = $row[6];
		 $rec_source = $row[7];
		 $rec_qm = $row[8];
		 $rec_ingred = $row[9];
		 $rec_direct = $row[10];
		 $rec_notes = $row[11];
    }
//return $rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes;
}
		 
if ($mode == NULL) {
   ?>
   <ul>
   	   <li><a href="./post.php?mode=add">Add a Recipe</a></li>
	   <li><a href="./post.php?mode=show">Show List of Recipes for Editing</a></li>
	   <li><a href="./post.php?mode=del">Show List of Recipes for Deleting</a> - ! Broken !</li>
	   <li>Edit by Recipe ID:<form method="GET" action="./post.php?mode=edit"><input type="text" name="rec_id" size="4"><input type="submit" value="EDIT"></li>
   </ul> 
   <?php
}
elseif ($mode == "add") {
   function form($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
}
elseif (&mode == "ins") {
   function post_var($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
   function insert_sql($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
}
elseif ($mode == "edt") {
   function get_sql($rec_id);
   function form($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
}
elseif ($mode == "upd") {
   function post_var($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
   function update_sql($rec_id, $rec_name, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_source, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
}
elseif ($mode == "show") {
   $query = "SELECT * FROM recipe ORDER BY rec_id";
   $result = mysql_query($query) or die(mysql_error());
   while ($row = mysql_fetch_array($result)) {
		 $rec_id = $row[0];
    	 $rec_name = $row[1];
		 $rec_cat = $row[2];
		 $rec_main = $row[3];
		 $rec_prep = $row[4];
		 $rec_cook = $row[5];
		 $rec_serves = $row[6];
		 $rec_source = $row[7];
		 $rec_qm = $row[8];
		 $rec_ingred = $row[9];
		 $rec_direct = $row[10];
		 $rec_notes = $row[11];
		 
		 echo "<a href=\"./post.php?mode=edt&rec_id=" .$rec_id. "\">" .$rec_name. "</a> - " .$rec_cat. "<br />\n<br />\n"; 
	}
elseif ($mode == "del") {
   $query = "DELETE FROM recipe WHERE rec_id=$rec_id";
   $result = mysql_query($query) or die(mysql_error());
}
else {

}
?>   